import cv2
import numpy as np
from matplotlib import pyplot as plt
In this Project, our goal is to do semantic segmentation with OpenCV lib, then using additional tools to detect QR codes and size of the shapes in the stream video.
NOTE: this instruction file has been created with helps of various library documents, we respect open source library access, specially OpenCV, NUMPY official docs and STACKOVERFLOW/GITHUB/LearnOCV forums.
https://docs.opencv.org/master/d6/d00/tutorial_py_root.html
https://docs.opencv.org/master/d7/dbd/group__imgproc.html
https://www.ccoderun.ca/programming/doxygen/opencv/tutorial_root.html
In this part, you should write a report with jupyter notebook about image filters, geometric transforamtions, video processing and their examples. read instructions carefully.
Here we have introduced some examples in order to warm you up :)
Once again, Object-oriented approach is one of the goals of this project!
Implement all of these functions in a class called ImageSignal. this should be like cell bellow. consider that you can use some other classes if you think that they enhance your implementation.
class ImageSignal:
def __init__(self):
self.property1 = [] # initializer
### TODO
return
def method_name(self, inputs):
# TODO
pass
return
@staticmethod
def static_method_name(inputs):
# TODO
pass
return
imgpath = 'images/PGN1.jpg'
img = cv2.imread(imgpath)
cv2.imshow('Pagani Zonda F', img)
cv2.waitKey(0)
img_ = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(20, 30))
plt.imshow(img_)
plt.axis(False)
plt.show()
scale_ = 40
width_ = int(img.shape[1]*scale_/100)
height_ = int(img.shape[0]*scale_/100)
dim_ = (width_, height_)
resized_img = cv2.resize(img, dim_, interpolation=cv2.INTER_AREA)
cv2.imshow('Pagani resized', resized_img)
cv2.waitKey(0)
print(img.shape, resized_img.shape)
imgpath_ = 'images/PGN2.jpg'
cv2.imwrite(imgpath_, resized_img)
First action in image processing is filteration. You may think that we will take FFT from image, but no :) there is a theorem that makes our work simple:
Theorem I: A Gaussian function's fourier transform in N-dimensional space is another gaussian with different $\sigma$ and $\mu$
So this means that instead of taking FFT and then filter somewhere in that, we are able to convolve filter kernels with image in rgb space. kernels are fxf matrices that represent filteration.
Include two examples of average filtering on arbitrary images in your report.
img = cv2.imread('images/LA1.jpg')
kernel = np.ones((7, 7), np.float32)/49
avg = cv2.filter2D(img, -1, kernel)
plt.figure(figsize=(30, 30))
plt.subplot(211)
plt.imshow(img)
plt.title('Original')
plt.xticks([])
plt.yticks([])
plt.subplot(212)
plt.imshow(avg)
plt.title('Averaging')
plt.xticks([])
plt.yticks([])
plt.show()
In the final report, include some examples of gaussian filtering on gaussian noisy images.
img = cv2.imread('images/LA1.jpg')
kernel10 = cv2.getGaussianKernel(ksize=7, sigma=10)
kernel1 = cv2.getGaussianKernel(ksize=7, sigma=1)
gsn1 = cv2.filter2D(img, -1, kernel10)
gsn2 = cv2.filter2D(img, -1, kernel1)
plt.figure(figsize=(30, 40))
plt.subplot(311)
plt.imshow(img)
plt.title('Original')
plt.xticks([])
plt.yticks([])
plt.subplot(312)
plt.imshow(gsn1)
plt.title('Gaussian with $\sigma$ = 1')
plt.xticks([])
plt.yticks([])
plt.subplot(313)
plt.imshow(gsn2)
plt.title('Gaussian with $\sigma$ = 10')
plt.xticks([])
plt.yticks([])
plt.show()
Median filtering is a non-linear filtering method that is sometimes more efficient than average methods.
In the report, include some examples of median filtering on salt-pepper noisy images with different kernels.
img = cv2.imread('images/LA1.jpg')
med = cv2.medianBlur(img, ksize=7)
plt.figure(figsize=(30, 40))
plt.subplot(211)
plt.imshow(img)
plt.title('Original')
plt.xticks([])
plt.yticks([])
plt.subplot(212)
plt.imshow(med)
plt.title('Median filter')
plt.xticks([])
plt.yticks([])
plt.show()